package org.example.service.impl;

import cn.hutool.core.util.IdUtil;
import org.example.mapper.FruitInboundMapper;
import org.example.mapper.FruitMapper;
import org.example.mapper.InventoryDetailMapper;
import org.example.mapper.InventoryMapper;
import org.example.domain.Fruit;
import org.example.domain.FruitInbound;
import org.example.domain.FruitInventory;
import org.example.domain.FruitInventoryDetail;
import org.example.service.FruitInboundService;
import org.example.util.ShortSnowflakeIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
public class FruitInboundServiceImpl implements FruitInboundService {

    private static final Logger logger = LoggerFactory.getLogger(FruitInboundServiceImpl.class);
    @Autowired
    private FruitInboundMapper fruitInboundMapper;
    @Autowired
    private InventoryMapper inventoryMapper;
    @Autowired
    private FruitMapper fruitMapper;

    @Autowired
    private InventoryDetailMapper inventoryDetailMapper;
    /**
     * 获取入库单列表
     */
    @Override
    public List<FruitInbound> getInboundList(String fruitName, String supplierName, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        // 计算偏移量
        Integer offset = (pageNum - 1) * pageSize;

        // 调用Mapper查询数据
        return fruitInboundMapper.selectInboundList(fruitName, supplierName, startTime, endTime, offset, pageSize);
    }

    /**
     * 获取入库单总数
     */
    public Integer getInboundCount(String fruitName, String supplierName, String startTime, String endTime) {
        return fruitInboundMapper.selectInboundCount(fruitName, supplierName, startTime, endTime);
    }

    @Override
    public FruitInbound getInboundById(Long id) {
        return fruitInboundMapper.selectById(id);
    }

    /***
     * 创建水果入库单
     * @return
     */
    @Override
    public int createInbound(FruitInbound inbound) {
        inbound.setNumber(String.valueOf(IdUtil.getSnowflakeNextId()));
        inbound.setCreateTime(new Date());
        inbound.setUpdateTime(new Date());
        return fruitInboundMapper.insert(inbound);
    }

    @Override
    public void deleteInbound(Long id) {
        fruitInboundMapper.deleteById(id);
    }
    /***
     * 拒绝入库
     * @param id
     */
    @Override
    public void rejectInbound(Long id) {
        fruitInboundMapper.changeStatusById(id,"2");
    }

    /***
     * 入库审核通过
     * @param id
     */
    @Transactional
    @Override
    public void approveInbound(Long id) {
        //雪花算法生成id
        ShortSnowflakeIdGenerator myId = new ShortSnowflakeIdGenerator(1); // 使用workerId=1

        //修改入库单状态为已入库
        fruitInboundMapper.changeStatusById(id,"1");

        //查询入库单信息
        FruitInbound fruitInbound = fruitInboundMapper.selectById(id);


        //根据水果名称查询该水果的品类信息
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("name",fruitInbound.getFruitName());
        List<Fruit> fruits = fruitMapper.selectByMap(objectObjectHashMap);
        Fruit theFruit=fruits.get(0);

        //根据入库单信息去库存中查询有没有对应商品
        HashMap<String, Object> selectMap = new HashMap<>();
        selectMap.put("fruit_name",fruitInbound.getFruitName());
        selectMap.put("category",fruitInbound.getCategory());
        selectMap.put("unit",fruitInbound.getUnit());
        List<FruitInventory> inventoryList = inventoryMapper.selectByMap(selectMap);
        if (inventoryList.size()==0){
            //新增一条库存主表信息
            FruitInventory fruitInventory = new FruitInventory();
            fruitInventory.setId(myId.nextId());
            fruitInventory.setFruitName(fruitInbound.getFruitName());
            fruitInventory.setCategory(fruitInbound.getCategory());
            fruitInventory.setImage(theFruit.getImage());
            fruitInventory.setUnit(fruitInbound.getUnit());
            fruitInventory.setUpdateTime(new Date());
            fruitInventory.setStock(fruitInbound.getQuantity());
            inventoryMapper.insert(fruitInventory);
            logger.info("新增库存主表");

            //新增库存子表信息
            FruitInventoryDetail fruitInventoryDetail = new FruitInventoryDetail();
            fruitInventoryDetail.setInventoryId(fruitInventory.getId());
            fruitInventoryDetail.setCategory(fruitInventory.getCategory());
            fruitInventoryDetail.setUnit(fruitInventory.getUnit());
            fruitInventoryDetail.setStock(fruitInbound.getQuantity());
            inventoryDetailMapper.insert(fruitInventoryDetail);
            logger.info("新增库存子表");
        }else {
            //库存主表信息
            FruitInventory fruitInventory = inventoryList.get(0);
            fruitInventory.setUpdateTime(new Date());
            fruitInventory.setStock(fruitInventory.getStock().add(fruitInbound.getQuantity()));
            //修改库存主表信息
            inventoryMapper.updateById(fruitInventory);
            logger.info("修改库存主表");

            //插入一条子表信息
            //新增库存子表信息
            FruitInventoryDetail fruitInventoryDetail = new FruitInventoryDetail();
            fruitInventoryDetail.setInventoryId(fruitInventory.getId());
            fruitInventoryDetail.setCategory(fruitInventory.getCategory());
            fruitInventoryDetail.setUnit(fruitInventory.getUnit());
            fruitInventoryDetail.setStock(fruitInbound.getQuantity());
            inventoryDetailMapper.insert(fruitInventoryDetail);
            logger.info("新增库存子表");
        }


    }
}